package uy.com.nfc.writer.adapters;

import java.util.List;

import uy.com.nfc.writer.MainActivity;
import uy.com.nfc.writer.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.nfctagcommons.classes.VigilancePoint;

public class LocalListAdapter extends ArrayAdapter<String> { 
	private static final int GRAY = Color.argb(238, 230, 230, 230);
	private static final int GRAY_PRESSED = Color.argb(238, 190, 190, 190);
	private List<VigilancePoint> pointsList;
	private ViewHolder lastPressedViewHolder;
	private MainActivity activity;
	private Typeface customTypeface;
	public LocalListAdapter(MainActivity activity, String[] items, List<VigilancePoint> pointsList) {
		super(activity.getApplicationContext(), R.layout.activity_main, items);
		this.activity = activity;
		this.pointsList = pointsList;
		customTypeface = Typeface.createFromAsset(activity.getAssets(), "fonts/AliquamREG.ttf");
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.view_local, parent, false);
			viewHolder = new ViewHolder();
		    viewHolder.localView = convertView;
		    viewHolder.localName = (TextView) convertView.findViewById(R.id.local_view_name);
		    convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.localView.setBackgroundColor(GRAY);
		final VigilancePoint point = pointsList.get(position);
		viewHolder.localName.setText(point.getLocalName());
		viewHolder.localName.setTypeface(customTypeface);
		final ViewHolder finalViewHolder = viewHolder;
		viewHolder.localView.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				activity.setLocal(point.getLocalId());
				if (lastPressedViewHolder != null){
					lastPressedViewHolder.localView.setBackgroundColor(GRAY);
				}
				finalViewHolder.localView.setBackgroundColor(GRAY_PRESSED);
				lastPressedViewHolder = finalViewHolder;
			}
		});
		return convertView; 
	}
	static class ViewHolder {
		protected View localView;
		protected TextView localName;
	}
}
